In [1]:
    
import numpy as np
import matplotlib.pyplot as pl
import seaborn as sn
from sklearn.cluster import MiniBatchKMeans
from sklearn import metrics
%matplotlib inline
    
In [37]:
    
stI=np.load('../data/stokesI_sunspot.npy')
stV=np.load('../data/stokesV_sunspot.npy')
wave = np.loadtxt('../data/wavelengthHinode.txt')
stI.shape
    
    Out[37]:
In [3]:
    
sn.set_style("dark")
f, ax = pl.subplots(figsize=(9,9))
ax.imshow(stI[:,:,0], aspect='auto', cmap=pl.cm.gray)
    
    Out[3]:
    
    
Let us compute simple things like the contrast and the Doppler velocity field
In [9]:
    
contrastFull = np.std(stI[:,:,0]) / np.mean(stI[:,:,0])
contrastQuiet = np.std(stI[400:,100:300,0]) / np.mean(stI[400:,100:300,0])
print("Contrast in the image : {0}%".format(contrastFull * 100.0))
print("Contrast in the quiet Sun : {0}%".format(contrastQuiet * 100.0))
    
    
Now let us compute the velocity field. To this end, we compute the location of the core of the line in velocity units for each pixel.
In [31]:
    
v = np.zeros((512,512))
for i in range(512):
    for j in range(512):
        pos = np.argmin(stI[i,j,20:40]) + 20
        res = np.polyfit(wave[pos-2:pos+2], stI[i,j,pos-2:pos+2], 2)       
        w = -res[1] / (2.0 * res[0])
        v[i,j] = (w-6301.5) / 6301.5 * 3e5
    
In [34]:
    
f, ax = pl.subplots(figsize=(9,9))
ax.imshow(np.clip(v,-5,5))
f.savefig('velocities.png')
    
    
In [6]:
    
f, ax = pl.subplots(nrows=1, ncols=2, figsize=(15,9))
ax[0].imshow(stI[:,0,:], aspect='auto', cmap=pl.cm.gray)
ax[1].imshow(stV[:,0,:], aspect='auto', cmap=pl.cm.gray)
    
    Out[6]:
    
In [8]:
    
f.savefig('exampleStokes.png')
    
In [17]:
    
X = stV[50:300,200:450,:].reshape((250*250,112))
    
In [22]:
    
maxV = np.max(np.abs(X), axis=1)
X = X / maxV[:,None]
    
In [40]:
    
nClusters = 9
km = MiniBatchKMeans(init='k-means++', n_clusters=nClusters, n_init=10, batch_size=500)
    
In [41]:
    
km.fit(X)
    
    Out[41]:
In [42]:
    
out = km.predict(X)
    
In [43]:
    
avg = np.zeros((nClusters,112))
for i in range(nClusters):
    avg[i,:] = np.mean(X[out==i,:], axis=0)
    
In [44]:
    
f, ax = pl.subplots(ncols=3, nrows=3, figsize=(12,9))
loop = 0
for i in range(3):
    for j in range(3):
        percentage = X[out==i,:].shape[0] / (250*250.) * 100.0        
        ax[i,j].plot(km.cluster_centers_[loop,:])
        ax[i,j].set_title('Class {0} - {1}%'.format(loop, percentage))
        loop += 1
pl.tight_layout()
    
    
In [29]:
    
    
    Out[29]:
In [ ]: